export type MeetingMode = "join" | "share_screen" | "share_display";

export interface MeetingProps {
    mode: MeetingMode;
}

/// 如果是分享模式的话，需要建立新的 RTCPeerConnection
export type MeetingMsgNewIn = {
    new_in: { user_id: number, name: string }
}

export type MeetingMsgIceCandidate = {
    ice_candidate: {
        tag: 'send' | 'recv' // 发送信息端的类型
        data: RTCIceCandidate
        user_id: number
    }
}

export type MeetingMsgOffer = {
    offer: {
        data: any
        user_id: number
    }
}

export type MeetingMsgAnswer = {
    answer: {
        data: any
        user_id: number
    }
}

export type MeetingMsgDisconnection = {
    disconnection: {
        user_id: number
    }
}

export type MeetingMsgYourId = {
    /// 这里大写的原因是, 这个东西在 服务器端代码中是一个类型
    YourId: {
        id: number
    }
}

export type MeetingMsgOther = {
    other: {
        id: number,
        name: string
    }
}

export type MeetingMsgNewRtcPeerConnection = {
    new_rtc_peer_connection: {
        id: number, // 发起者的 id
    }
}
export type MeetingMsgDisconnectPeer = {
    disconnect_peer: {
        tag: 'send' | 'recv'
        id: number // 需要断开的 connection 的 id
    }
}

export type MeetingMsgOneExit = {
    OneExit: {
        id: number
    }
}

export type MeetingMsg = MeetingMsgDisconnectPeer
    | MeetingMsgNewRtcPeerConnection
    | MeetingMsgNewIn
    | MeetingMsgAnswer
    | MeetingMsgOffer
    | MeetingMsgIceCandidate
    | MeetingMsgDisconnection
    | MeetingMsgYourId
    | MeetingMsgOther
    | MeetingMsgOneExit

export type Forward = BroadCast | ToOther

export type BroadCast = {
    Broadcast: { msg: any }
}
export type ToOther = {
    ToOther: {
        user_id: number,
        msg: any
    }
}

export function broadcast(msg: MeetingMsg): BroadCast {
    return {
        Broadcast: {
            msg
        }
    }
}
export function to_other(user_id: number, msg: MeetingMsg): ToOther {
    return {
        ToOther: {
            user_id,
            msg
        }
    }
}

export function is_object(ori: string): boolean {
    for (const char of ori) {
        if (char === ' ') continue
        else if (char === '{') return true
        else return false
    }
    return false
}
export function json_type(obj: any): string {
    return Object.keys(obj)[0]
}

export const NoRoom = "NoRoom"
export const NeedPassword = "NeedPassword"
export const WrongPassword = "WrongPassword"
export const WrongPasswordTimesOverLimit = "WrongPasswordTimesOverLimit"